package org.example.curriculum_design_6_16.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.curriculum_design_6_16.entity.pojo.Role;
import org.example.curriculum_design_6_16.mapper.UserRoleMapper;
import org.example.curriculum_design_6_16.service.RoleService;
import org.example.curriculum_design_6_16.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 郑凯
* @description 针对表【role】的数据库操作Service实现
* @createDate 2025-06-16 13:21:30
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{


    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 为用户分配默认角色(普通用户)
     * @param userId 用户ID (String类型)
     */
    public void assignDefaultRole(String userId) {
        // 普通用户角色ID为"2" (根据您数据库中的初始化数据)
        String userRoleId = "2";

        // 检查是否已分配
        if (!userRoleMapper.existsUserRole(userId, userRoleId)) {
            userRoleMapper.assignRole(userId, userRoleId);
        }
    }

    /**
     * 为用户分配指定角色
     * @param userId 用户ID
     * @param roleId 角色ID
     */
    public void assignRole(String userId, String roleId) {
        if (!userRoleMapper.existsUserRole(userId, roleId)) {
            userRoleMapper.assignRole(userId, roleId);
        }
    }

    /**
     * 移除用户的指定角色
     * @param userId 用户ID
     * @param roleId 角色ID
     */
    public void removeRole(String userId, String roleId) {
        userRoleMapper.removeRole(userId, roleId);
    }

    /**
     * 清除用户所有角色
     * @param userId 用户ID
     */
    public void clearUserRoles(String userId) {
        userRoleMapper.clearUserRoles(userId);
    }

}




